// ---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "sudoku_board.h"
#include "Unit2.h"

// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;

// ---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner) : TForm(Owner)
{

}

// ---------------------------------------------------------------------------
void __fastcall TForm3::Button1Click(TObject *Sender)
{
	if (Button1->Caption == "Set Up")
	{


		// Offset for boxex
		int offset = 52;
		// vertical offset, used for spacing quadrants
		int vbump = 100;
		// horizontal offset, used for spacing quadrants
		int hbump = 100;

		for (int j = 0; j < 9; j++)
		{
			if (j > 2 && j < 6)
			{
				hbump = 110;
			}
			else if (j > 5)
			{
				hbump = 120;
			}
			else
			{
				hbump = 100;
			}
			for (int i = 0; i < 9; i++)
			{
				if (MyEdit[j][i] == NULL)
					// only create the object if it does not exist
				{
					if (i > 2 && i < 6)
					{
						vbump = 110;
					}
					else if (i > 5)
					{
						vbump = 120;
					}
					else {
						vbump = 100;
					}

					MyEdit[j][i] = new TMyEdit(this);
					MyEdit[j][i]->Parent = this;
					MyEdit[j][i]->Top = vbump + (i * offset);
					MyEdit[j][i]->Left = hbump + (j * offset);
					MyEdit[j][i]->setLocation(j, i);
					MyEdit[j][i]->Font->Color = Form3->ColorDialog1->Color;
				}
				else // object already here so just reset the state of the object
				{
					MyEdit[j][i]->Enabled = true;
					MyEdit[j][i]->Text = "";
				}

			}

		}
		Button1->Caption = "Play";
		Label3->Caption = "Place Initial numbers, click Play button to play puzzle";
	}
	else if (Button1->Caption == "Play")
	{
		for (int i = 0; i < 9; i++)
		{
			for (int j = 0; j < 9; j++)
			{
				if (MyEdit[i][j]->getsetUp() == true)
				{
					// MyEdit[i][j]->MyEditChange(Sender);
					// MyEdit[i][j]->setInitialColor(ColorDialog1->Color);
					MyEdit[i][j]->ReadOnly = true;
			  //		MyEdit[i][j]->Color = Form3->getInitialColor();

				}
			}
		}
		Button1->Caption = "Reset Board";
		Label3->Caption = "Solve the puzzle, click reset board to clear the puzzle and start over";
	   //	ShowMessage("TEST");
	}
	else
	{
		for (int i = 0; i < 9; i++)
		{
			for (int j = 0; j < 9; j++)
				{
				MyEdit[i][j]->setsetUp(false);
				MyEdit[i][j]->Text = "";
				MyEdit[i][j]->ReadOnly = false;
				MyEdit[i][j]->setAllNums();
                MyEdit[i][j]->Font->Color = ColorDialog1->Color;
				}
		}
		Form3->Button1->Caption = "Set Up";
		Form3->Button1Click(Form3->Button1);
	}
}
// ---------------------------------------------------------------------------

void __fastcall TForm3::Exit1Click(TObject *Sender)
{
	exit(1);
}
// ---------------------------------------------------------------------------

void __fastcall TForm3::InitialNumbers1Click(TObject *Sender)
{
	ColorDialog1->Execute();

//	ShowMessage(ColorDialog1->Color);
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (MyEdit[i][j]->getsetUp() == true)
			{
				// MyEdit[i][j]->MyEditChange(Sender);
				// MyEdit[i][j]->setInitialColor(ColorDialog1->Color);
				MyEdit[i][j]->Font->Color = ColorDialog1->Color;

			}
		}
	}
}
// ---------------------------------------------------------------------------
void __fastcall TForm3::NewNumbers1Click(TObject *Sender)
{

	ColorDialog2->Execute();

	ShowMessage("newnumbers1click");
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (MyEdit[i][j]->getsetUp() == false)
			{
				MyEdit[i][j]->Font->Color = ColorDialog2->Color;

			}
		}
	}
}
//---------------------------------------------------------------------------

int TForm3::getoldValue()
{
	return(oldValue);
}

//----------------------------------------------------------------------------
void TForm3::setoldValue(int newValue)
{
	oldValue = newValue;
}


//-----------------------------------------------------------------------------
void __fastcall TForm3::Button2Click(TObject *Sender)
{
	if (Button1->Caption != "Set Up")
	{
		if (Button2->Caption == "Hints On")
		{
			Button2->Caption = "Hints Off";
			for (int i = 0; i < 9; i++)
			{
				for (int j = 0; j < 9; j++)
				{
					MyEdit[i][j]->ShowHint = true;

				}
			}
		}
		else
		{
			Button2->Caption = "Hints On";
			for (int i = 0; i < 9; i++)
			{
				for (int j = 0; j < 9; j++)
				{
					MyEdit[i][j]->ShowHint = false;

				}
			}
		}
	}
	else
	{
    	ShowMessage("You can not turn on hints untill you have a board. Please click the Set Up button");
	}
}
//---------------------------------------------------------------------------

void __fastcall TForm3::Button3Click(TObject *Sender)
{

 // setup a test board

MyEdit[2][0]->Text = "3";
MyEdit[3][0]->Text = "1";
MyEdit[4][0]->Text = "8";
MyEdit[8][0]->Text = "2";
MyEdit[0][1]->Text = "9";
MyEdit[1][1]->Text = "1";
MyEdit[2][1]->Text = "4";
MyEdit[4][1]->Text = "3";
MyEdit[0][2]->Text = "6";
MyEdit[5][2]->Text = "4";
MyEdit[7][2]->Text = "3";
MyEdit[3][3]->Text = "4";
MyEdit[6][3]->Text = "6";
MyEdit[7][3]->Text = "1";
MyEdit[1][4]->Text = "7";
MyEdit[7][4]->Text = "9";
MyEdit[1][5]->Text = "4";
MyEdit[2][5]->Text = "6";
MyEdit[5][5]->Text = "9";
MyEdit[1][6]->Text = "9";
MyEdit[3][6]->Text = "3";
MyEdit[8][6]->Text = "6";
MyEdit[4][7]->Text = "7";
MyEdit[6][7]->Text = "1";
MyEdit[7][7]->Text = "8";
MyEdit[8][7]->Text = "3";
MyEdit[0][8]->Text = "5";
MyEdit[4][8]->Text = "1";
MyEdit[5][8]->Text = "8";
MyEdit[6][8]->Text = "4";



}
//---------------------------------------------------------------------------

